package com.rampage.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.util.Assert;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public abstract class BeanUtils extends org.springframework.beans.BeanUtils {
    static Logger mlogger = LogManager.getLogger(BeanUtils.class);

    /**
     * 拷贝属性
     *
     * @param source
     * @param target
     * @param excludeCopyNull 是否排除 拷贝null
     * @throws BeansException
     */
    public static void copyProperties(Object source, Object target,
                                      boolean excludeCopyNull) {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        for (PropertyDescriptor targetPd : targetPds) {
            if (targetPd.getWriteMethod() != null) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source
                        .getClass(), targetPd.getName());
                if (sourcePd != null && sourcePd.getReadMethod() != null) {
                    try {
                        Method readMethod = sourcePd.getReadMethod();
                        if (!Modifier.isPublic(readMethod.getDeclaringClass()
                                .getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        Object value = readMethod.invoke(source);
                        // 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等
                        if (value != null) {
                            Method writeMethod = targetPd.getWriteMethod();
                            if (!Modifier.isPublic(writeMethod
                                    .getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        }
                    } catch (Exception ex) {
                        mlogger.error("Could not copy properties from source to target", ex);
                    }
                }
            }
        }
    }

    /**
     * 获取javabean属性
     * <p>
     * {@link org.springframework.beans.BeanUtils#getPropertyDescriptors(Class)}
     *
     * @param beanClass
     * @return
     */
    @Deprecated
    public static PropertyDescriptor[] getBeanProperty(Class beanClass) {
        PropertyDescriptor[] props = null;
        try {
            props = Introspector.getBeanInfo(beanClass, Object.class)
                    .getPropertyDescriptors();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        return props;
    }

    /**
     * copy 属性
     *
     * @param collection
     * @param objectClass 必须含有一个 无参数的 构造方法
     * @return
     */
    public static <T> Set copyProperties(Set collection, Class<T> objectClass) {
        return (Set) copyProperties((Collection) collection, objectClass);
    }

    /**
     * copy 属性
     *
     * @param collection
     * @param objectClass 必须含有一个 无参数的 构造方法
     * @return
     */
    public static <T> List copyProperties(List collection, Class<T> objectClass) {
        return (List) copyProperties((Collection) collection, objectClass);
    }

    /**
     * copy 属性
     *
     * @param collection
     * @param objectClass 必须含有一个 无参数的 构造方法
     * @return
     */
    public static <T> Collection<T> copyProperties(Collection collection, Class<T> objectClass) {
        Collection outCollection = instantiate(collection.getClass());
        for (Object src : collection) {
            Object target = instantiate(objectClass);
            copyProperties(src, target);
            outCollection.add(src);
        }
        return outCollection;
    }


}